#include "app_runner.h"
#include "app_router.h"
#include "app_serial.h"
#include "app_pool.h"
#include "log/log.h"
#include <unistd.h>
#include <signal.h>
#include "app_message.h"

static int running = 1;

void app_runner_stop(int sig)
{
    running = 0;
}

void app_runner_run(void)
{
    // 初始化
    signal(SIGINT, app_runner_stop);
    signal(SIGTERM, app_runner_stop);
    if (app_router_init() < 0)
    {
        log_error("app_router_init failed");
        return;
    }

    SerialDevice *serial_device = app_serial_init("/dev/ttyS1");
    if (app_serial_setConnectionType(serial_device, CONN_BT) < 0)
    {
        log_warn("app_serial_setConnectionType failed");
    }

    if (serial_device == NULL)
    {
        log_error("app_serial_init failed");
        return;
    }
    app_pool_init(5);

    app_router_register_device((Device *)serial_device);

    unlink("/tmp/gateway.invalid");

    while (running)
    {
        sleep(1);
    }

    // 资源清理
    app_router_close();
    app_pool_close();
}
